// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameData_red_blue_man, {emitType} from "./GameData_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import ControlScript from "../PublicScript/ControlScript";
import Share from "./Share";
import LoadShow from "./LoadShow";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Resurgence extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Sprite)
    sprite: cc.Sprite = null;

    start () {
        this.schedule(this.show, 0.01, 499)
    }

    initCount = 5;
    index = 0;
    count = 0;
    show() {
        this.index += 1;
        this.sprite.fillRange -= 0.002;
        if(this.index % 100 == 0){
            this.label.string = (this.initCount - this.index/100) + ""
        }
        if(this.index == 500){
            //失败逻辑
            this.loadOver();
        }
    }

    click(e, d){
        let self = this;
        Music.GamePlayEffect(musicName.click);
        this.unschedule(this.show);
        if(parseInt(d) == 1){
            //复活逻辑
            GameData_red_blue_man.showVideo({callback:()=>{
                    cc.game.emit(emitType.resurgence);
                    this.node.destroy();
                }, callback1: ()=>{
                    this.loadOver();
                    ControlScript.createToast("需看完视频才能复活!");
                }, name: "视频复活"});
        }
        if(parseInt(d) == 2){
            this.loadOver();
        }
    }


    //加载结算界面
    loadOver(){
        // ControlScript.instantiate("share", (view)=>{
        //     view.getComponent(Share).setCallback(false);
        // });
        ControlScript.instantiate("loadShow", (view)=>{
            view.getComponent(LoadShow).show("gameOver", true,true);
            this.node.destroy();
        })
        this.node.destroy()
    }
}
